from pgm import *;
from subprocess import call;

g = Graph();
me = Node("MEx");
n = Node("Null");
r = Node("Run");
n2 = Node("Null2");

# g.addnode(n, [], [0.5]);
# g.addnode(r, [], [0.5]);
# g.addnode(me, [n,r], [0, 1, 1, 0]);
# g.addnode(n2, [n,r], [0, 1, 1, 0]);

g.addnode(n, [], [0.2]);
g.addnode(r, [], [0.8]);
g.addnode(me, [n,r], [0, 0.05, 0.9, 0]);
g.addnode(n2, [n,r], [0, 0.95, 0.1, 0]);
g.addobs(n, False);
g.addobs(r, True);
g.addobs(me, True);
g.setup();

print g.priorprob([n, r, me, n2]);
print g.postprob([n, r, me, n2]);
aMAP = g.getMAP();

# for i,node in enumerate(g.aNodes):
#     print aMAP[i], node.name;
print me.name, aMAP[g.node2label[me]];
print n.name, aMAP[g.node2label[n]];


# g.write2dot(fname='./BlackBoard/InfBB-graph.dot');
# call(['dot', '-Tpdf', './BlackBoard/InfBB-graph.dot', '-o', './BlackBoard/InfBB-graph.pdf']);
# call(['evince', './BlackBoard/InfBB-graph.pdf']);
























































# # g = Graph();
# # me = Node("MEx");
# # n = Node("Null");
# # r = Node("Run");
# # ch = Node("Chase");
# # obs1 = Node("Obs1");
# # obs2 = Node("Obs2");
# # # me2 = Node("MEx2");
# # n2 = Node("Null2");
# # r2 = Node("Run2");
# # ch2 = Node("Chase2");
# # # obs3 = Node("Obs3");
# # 
# # # g.addnode(me, [n,r,ch], [0, 1, 1, 0, 1, 0, 0, 0]);
# # g.addnode(me, [n,r], [0, 1, 1, 0]);
# # g.addnode(n, [], [0.5]);
# # g.addnode(r, [], [0.5]);
# # g.addnode(ch, [], [0.5]);
# # # g.addnode(obs1, [r], [0.1, 0.3]);
# # # g.addnode(obs2, [ch], [0.1, 0.5]);
# # 
# # # g.addnode(me2, [n2,ch2], [0, 1, 1, 0]);
# # # g.addnode(n2, [n,r,ch], [0, 0.06, 1, 0, 1, 0, 0, 0]);
# # g.addnode(n2, [n,r], [0, 1, 0.1, 0]);
# # # g.addnode(ch2, [ch], [0, 0.93]);
# # # g.addnode(obs3, [ch2], [0.04, 0.0080]);
# # g.setup();
# # 
# # g.addobs(me, True);
# # # g.addobs(me2, True);
# # # g.addobs(obs1, True);
# # # g.addobs(obs2, True);
# # # g.addobs(obs3, True);
# # # g.addobs(n,False);
# # 
# # # print g.priorprob([n, r, ch]);
# # print g.postprob([n, r, ch]);
# # print g.postprob([n2]);
# # 
# # # g.write2dot(fname='./InferenceBB/InfBB-graph.dot');
# # # call(['dot', '-Tpdf', './InferenceBB/InfBB-graph.dot', '-o', './InferenceBB/InfBB-graph.pdf']);
# # # call(['evince', './InferenceBB/InfBB-graph.pdf']);